package com.example.usergroup.viewmodel;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.example.mvvmcommon.mvvm.viewmodel.BaseViewModel;
import com.example.networkmoudel.entity.BaseEntity;
import com.example.usergroup.data.entity.UserEventEntity;
import com.example.usergroup.model.ResetModel;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class ResetViewModel extends BaseViewModel<ResetModel> {

    public ObservableField<String>pwd=new ObservableField<>();
    public ObservableField<String>pwd2=new ObservableField<>();
    public String phone;
    @Override
    protected void initData() {

    }

    @Override
    protected ResetModel createModel() {
        return new ResetModel();
    }

    @Override
    protected void sucessFull(BaseEntity entity) {
        if (entity.getStatus()==0){
            UserEventEntity userEventEntity = new UserEventEntity();
            userEventEntity.action = "update";
            userEventEntity.phone = phone;
            userEventEntity.pwd = pwd.get();
            EventBus.getDefault().post(userEventEntity);
            UserEventEntity finishEntity = new UserEventEntity();
            finishEntity.action = "finish";
            EventBus.getDefault().post(finishEntity);
            getUiChangeLiveData().getFinishActivity().setValue(""+System.currentTimeMillis());
        }
    }

    public void resetbnt(){
        if (!pwd.get().equals(pwd2.get())){
            showMsg("两次密码不一样");
            return;
        }
        LogUtils.e("phone:"+phone);
        Map<String,Object> map = new HashMap<>();
        map.put("mobile",phone);
        map.put("pwd",pwd.get());
        m.requestReset(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void toBack(){
        getUiChangeLiveData().setFinishActivity(""+System.currentTimeMillis());
    }
}
